package com.yogee.topseller.fragment.collect;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpFragment;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.IntelligentScreeningListRvAdapter;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/3 0003 9:27
 * 创建人：wangye
 * 修改备注：
 */

public class CollectBodyFragment extends HttpFragment implements RefreshUtil.OnRefreshListener {
    @BindView(R.id.body_recycler)
    EmptyRecyclerView bodyRecycler;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;
    @BindView(R.id.body_refresh)
    TwinklingRefreshLayout bodyRefresh;
    private IntelligentScreeningListRvAdapter adapter;
    private String collectType;
    private List<InterlligentFilterBean.ProductListBean> been;
    private boolean isRefresh = false;
    private int collectTypeH = 0;//收藏分类，0收藏，1分享，2浏览

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collect_body;
    }

    @Override
    protected void initView() {
        been = new ArrayList<>();
        MySinaRefreshView headerView = new MySinaRefreshView(getActivity());
        bodyRefresh.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        bodyRefresh.setHeaderView(headerView);
        bodyRefresh.setBottomView(new RefreshView(getActivity()));
        bodyRefresh.setBottomHeight(50);
        adapter = new IntelligentScreeningListRvAdapter(getActivity());
        if(getArguments().getInt("collectType")==0){
            iv_empty.setImageResource(R.drawable.nodata7);
        }else{
            iv_empty.setImageResource(R.drawable.nodata9);
        }

        bodyRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        bodyRecycler.setHasFixedSize(true);
        bodyRecycler.setAdapter(adapter);
        bodyRecycler.setEmptyView(id_empty_view);
        isRefresh = true;


    }

    @Override
    public void onResume() {
        super.onResume();
        isRefresh = true;
        if (null != getArguments()) {
            collectTypeH = getArguments().getInt("collectType");
            if (0 == collectTypeH) {
                //判断登录状态，已经登录则获取数据
                if (AppUtil.isExamined(getActivity())) {
                    collectRecord(AppUtil.getUserId(getActivity()), "0", "10");
                }

            } else if (1 == collectTypeH) {
                //判断登录状态，已经登录则获取数据
                if (AppUtil.isExamine(getActivity())) {
                    //暂时去掉的分享
                    shareRecord(AppUtil.getUserId(getActivity()), "0", "10");
                }
            } else if (2 == collectTypeH) {
                //判断登录状态，已经登录则获取数据
                if (AppUtil.isExamine(getActivity())) {

                    lookRecord(AppUtil.getUserId(getActivity()), "0", "10");
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                bodyRefresh.finishRefreshing();
            }
        }, 1500);
        if (AppUtil.isExamined(getActivity())) {
            if (0 == collectTypeH) {
                collectRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (1 == collectTypeH) {
                //暂时去掉的分享
                shareRecord(AppUtil.getUserId(getActivity()), "0", "10");
            } else if (2 == collectTypeH) {
                lookRecord(AppUtil.getUserId(getActivity()), "0", "10");
            }
        }
    }

    @Override
    public void onLoad() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                bodyRefresh.finishLoadmore();
            }
        }, 1500);
        isRefresh = false;
        if (AppUtil.isExamined(getActivity())) {
            if (0 == collectTypeH) {
                collectRecord(AppUtil.getUserId(getActivity()), been.size() + "", "10");
            } else if (1 == collectTypeH) {
                //暂时去掉的分享
                shareRecord(AppUtil.getUserId(getActivity()), been.size() + "", "10");
            } else if (2 == collectTypeH) {
                lookRecord(AppUtil.getUserId(getActivity()), been.size() + "", "10");
            }
        }
    }

    /**
     * 获取收藏列表
     *
     * @param userId
     * @param total
     * @param count
     */
    private void collectRecord(String userId, String total, String count) {
        HttpManager.getInstance().collectRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
            @Override
            public void onNext(InterlligentFilterBean o) {
                CollectBodyFragment.this.loadingFinished();
                if (isRefresh) {
                    been.clear();
                    been = o.getProductList();
                    bodyRefresh.finishRefreshing();
                } else {
                    been.addAll(o.getProductList());
                    bodyRefresh.finishLoadmore();
                }
                adapter.addData(been);
            }
        }, this));
    }

    /**
     * 获取分享记录列表
     *
     * @param userId
     * @param total
     * @param count
     */
    private void shareRecord(String userId, String total, String count) {
        HttpManager.getInstance().shareRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
            @Override
            public void onNext(InterlligentFilterBean o) {
                CollectBodyFragment.this.loadingFinished();
                if (null != o.getProductList() && o.getProductList().size() > 0) {
                    if (isRefresh) {
                        been.clear();
                        been = o.getProductList();
                        bodyRefresh.finishRefreshing();
                    } else {
                        been.addAll(o.getProductList());
                        bodyRefresh.finishLoadmore();
                    }
                    adapter.addData(been);
                }
            }
        }, this));
    }

    /**
     * 获取浏览记录列表
     *
     * @param userId
     * @param total
     * @param count
     */
    private void lookRecord(String userId, String total, String count) {
        HttpManager.getInstance().lookRecord(userId, total, count)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
            @Override
            public void onNext(InterlligentFilterBean o) {
                CollectBodyFragment.this.loadingFinished();
                if (isRefresh) {
                    been.clear();
                    been = o.getProductList();
                    bodyRefresh.finishRefreshing();
                } else {
                    been.addAll(o.getProductList());
                    bodyRefresh.finishLoadmore();
                }
                adapter.addData(been);
            }
        }, this));
    }

    @Override
    public void loadingFinished() {
        super.loadingFinished();
        if(pd!=null&&pd.isShowing())
            pd.dismiss();
    }
}
